https://introtcs.org/public/lec_02_representation.html#representing-objects-beyond-numbers
์ซ์ ์ธ์ ๋ค๋ฅธ ์๋ฃ์ ํํ
encoding, decoding ํจ์๋ฅผ ๊ฐ๊ฐ E,D๋ผ๊ณ ํ์.
๊ทธ๋ฌ๋ฉด ์ด๋ค ๊ฐ์ฒดobject๋ฅผ ํํํ๋ representation scheme์ (E,D)๋ค.
E๋ ๊ฐ์ฒด๋ฅผ 0,1์ ์ ํ๊ธธ์ด ์์ด๋ก, D๋ 0,1์ ์ ํ๊ธธ์ด ์์ด์ ๊ฐ๊ฐ ๋งตํํด์ฃผ๋ ํจ์.
๊ทธ๋ฆฌ๊ณ ๊ฐ์ฒด์ ๋ถ๋ถ์ o๋ผ๊ณ ํ๊ณ ๋ชจ๋ o ๋๋ฆฌ o = D(E(o)) ์ฌ์ผ ํ๋ค.
์ด๊ฒ D๊ฐ onto, ์ ์ฌํจ์๋ผ๋ ๊ฒ์ ํจ์ํ๋ค.
์?
D์ ์ ์์ญ์ ์ด์ง์์ด์ด๋ค. o1,o2,o3 ์ E๊ฐ 00,01,10 ์ผ๋ก ๋ณํํ๋ค๋ฉด 00,01,10์ ๊ฐ๊ฐ o๋ค๋ก ๋ณํํ ์ ์๊ฒ ์ง๋ง 1001110์ ๋ณํํ o๊ฐ ์๋ค(๋ถ๋ถํจ์).
๊ทธ๋์ ์ ์์ญ์ด ํฌ๊ธฐ๋๋ฌธ์ ์ ์ฌํจ์. ๋ฐ๋๋ก E๋ ๋จ์ฌํจ์๋ค.
Finite representations
๊ฐ์ฒด O๊ฐ ์ ํํ๋ค๋ฉด, ์์ด์ ์ ํํ ๊ธธ์ด n์ ๊ฐ์ง๋ค. n์๋ฆฌ์ ์ด์ง ์์ด์ ๊ฒฝ์ฐ์ ์๋ 2^{n+1}-1๊ฐ๋ค.
S= {s0,s1,s2,....,sk} , T={t0,t1,t2,.....tm}
์งํฉ S,T๊ฐ ์๊ณ E : S -> T ์ E๊ฐ ๋จ์ฌํจ์๋ผ๋ฉด, S์ ํฌ๊ธฐ๊ฐ T์ดํ์ฌ์ผ ํ๋ค.(์ ์์)
์ ์์ ๋จ์ฌ ํจ์๋ ์์ ์ ์ ์์ญ์ด ์ค๋ณต๋์ง ์๊ฒ ์น์ญ๊ณผ 1:1 ์ฐ๊ฒฐ์ด ๋์ด์ผ ํ๋๊น.
Prefix-free encoding
prefix๋?
y์ y๊ฐ ์์๋ y<=y
๋ฉด์ y์ i๋ฒ์งธ ๊น์ง y์ i๋ฒ์งธ์ ๊ฐ์ผ๋ฉด y๋ y
์ prefix๋ค.
"Hello"์ "Hello,World"๊ฐ ์์ผ๋ฉด Hello๊ฐ prefix๋ผ๋ ๋ง.
prefix-free?
๋ง ๊ทธ๋๋ก prefix๊ฐ ์๋ ์ํ๋ฅผ ์๋ฏธํ๋ค. O์ ๋ถ๋ถ๋ค์ธ o๋ฅผ E(o)ํ์ ๋, ์ด๋ค E(o)๋ E(o`)์ prefix๊ฐ ์๋๋.
prefix-free implies tuple encoding.
์ธ์ฝ๋ฉ ํจ์ E๊ฐ ๊ฐ์ฒด O๋ฅผ ์ด์ง์์ด๋ก ๋ณํํ๊ณ prefix-freeํ๋ค๋ฉด, ๋ณํ๋ ๊ฐ ์ด์ง์์ด์ ์์์ ๋ง๊ฒ ์ฐ๊ฒฐํ๋๊ฒ๊ณผ ๋๊ฐ๋ค.
o1,o2,o3์ E๊ฒฐ๊ณผ๊ฐ์ด ๊ฐ๊ฐ 0,10,110 ์ด๋ฉด, E(o1,o2,o3) = E(o1)E(o2)E(o3) = 010110.
๊ทธ๋ฆฌ๊ณ E(o1,o2,o3) 010110 ๊ณผ 1-1 ๊ด๊ณ์ผ ์ ๋ฐ์ ์๋ค.
์?
๊ฐ๋ตํ๊ฒ ๋งํ๋ฉด prefix๊ฐ ์์ผ๋๊น ์ค๋ณต์ด ์์ด์ 010110์ ํด๋
ํ๋ฉด์ 01/01/10์ธ์ง 0/101/10์ธ์ง ๋ช
ํํ์ง ์์ผ๋๊น. ๋ช
ํํ์ง ์์์ 1-n๊ด๊ณ.
prefix-free๋ก ๋ง๋ค๋ ค๋ฉด?
์ด๋ค๊ฑด ์์ฐ์ค๋ฝ๊ฒ prefix-free์ํ์ผ ์ ์๋ค. ๊ฐ๋ น ๋ชจ๋ ์ธ์ฝ๋ฉ ๊ฒฐ๊ณผ๋ฌผ์ ๊ธธ์ด๊ฐ ๋๊ฐ๋ค๋ฉด ์ด๋ค๊ฒ๋ ๋ค๋ฅธ ์ด๋ค๊ฒ์ prefix๊ฐ ๋์ง ์๋๋ค.
prefix์ ์์ y๊ฐ y์ ๊ธธ์ด ์ดํ์ด๋ฉด์, y
๊ฐ y๋ฅผ ์์ ํ ํฌํจํด์ผ prefix๊ฐ ๋๋ค. ๊ทธ๋ฐ๋ฐ ๋ชจ๋ ์ธ์ฝ๋ฉ์ด ๊ธธ์ด๊ฐ ๊ฐ์ผ๋ฉด์ ์ด๋ค๊ฒ์ด prefix๊ฐ ๋๋ค๋๊ฑด ๋ ์ธ์ฝ๋ฉ์ด ๋์ผํ๋ค๋ ๊ฒ์ด๊ณ , ์ด๊ฑด ์ธ์ฝ๋ฉ ํจ์ E๊ฐ 1-1 ํจ์๊ฐ ์๋๋ผ๋ ๊ฒ์ผ๋ก ์ธ์ฝ๋ฉ ํจ์์ ์ ์์ ๋ถํฉํ์ง ์๋๋ค.
์ผ๋จ ์์์ rational numbers๋ฅผ ๋ณํํ๋๊ฒ ์ฒ๋ผ 0->00 ์ผ๋ก 1->11๋ก ํ๋ ๋ฐฉ๋ฒ์ด ์๋ค.
์ฆ๋ช
:
- E๊ฐ 1-1 ํจ์์ด๋ฉด์
- E๊ฐ prefix-free๋ผ๋ฉด (์ฌ์ค prefix-free๋ผ๋ฉด ์ด๋ฏธ 1-1ํจ์๋ค.)
# takes functions encode and decode mapping
# objects to lists of bits and vice versa,
# and returns functions pfencode and pfdecode that
# maps objects to lists of bits and vice versa
# in a prefix-free way.
# Also returns a function pfvalid that says
# whether a list is a valid encoding
def prefixfree(encode, decode):
def pfencode(o):
L = encode(o)
return [L[i//2] for i in range(2*len(L))]+[0,1]
def pfdecode(L):
return decode([L[j] for j in range(0,len(L)-2,2)])
def pfvalid(L):
return (len(L) % 2 == 0 ) and all(L[2*i]==L[2*i+1] for i in range((len(L)-2)//2)) and L[-2:]==[0,1]
return pfencode, pfdecode, pfvalid
pfNtS, pfStN , pfvalidN = prefixfree(NtS,StN)
NtS(234)
# 11101010
pfNtS(234)
# 111111001100110001
pfStN(pfNtS(234))
# 234
pfvalidM(pfNtS(234))
# true
์ฌ๋ฌ๊ฐ์ง ์ธ์ฝ๋ฉ๋ค
๋ฌธ์ ์ธ์ฝ๋ฉ
ASCII ์ฝ๋
์์คํค์ฝ๋๋ ์์ด ์ํ๋ฒณ์ ํฌํจํ 128๊ฐ ๋ฌธ์๋ฅผ ๊ณ ์ ๋ 7๋นํธ ๊ธธ์ด๋ก ํํํ๋ค.
์์์ ๋ณธ๊ฒ์ฒ๋ผ ๊ธธ์ด๊ฐ ๋ชจ๋ ๋์ผํ ์ธ์ฝ๋ฉ์ ์์ฐํ Prefix-free๋ค.
Unicode
UTF-8 ๋ฐฉ๋ฒ์ ์ฌ์ฉํด์ ๋ฌธ์๋ฅผ ์ธ์ฝ๋ฉํ๋๋ฐ, ์ด๋ ASCII์ ๋ฌ๋ฆฌ ๊ฐ๋ณ ๊ธธ์ด๋ก prefix-free๋ฅผ ๊ตฌํํด์ ์ฌ์ฉํ๋ค.
Vectors, matrices, images ์ธ์ฝ๋ฉ
๋ฒกํฐ๋ ์ซ์๋ค์ ๋ฆฌ์คํธ์ด๊ณ ๋ฉํธ๋ฆญ์ค๋ ๋ฒกํฐ์ ๋ฆฌ์คํธ๋๊น ๋น์ฐํ ์ด์ง์๋ก ๋ํ๋ผ ์ ์๋ค.
์ด์ ๋น์ทํ๊ฒ ์ด๋ฏธ์ง๋ ๋ชจ๋ ํฝ์
๋ค์ด ๊ฐ์ง๊ณ ์๋ rgb๊ฐ์ ์ ์ฅํ๋ ๋ฐฉ์์ผ๋ก ์ธ์ฝ๋ฉํ ์ ์๊ฒ ๋ค.
ํ์ง๋ง ํจ์จ์ ์ด์ง ์์์ ์ ์ฌ์ฉํ์ง ์๋๋ค๊ณ . ๋ง์ด ์ฌ์ฉ๋๋ JPEG๋ฐฉ์์ ์ด๋ ๊ฒ ์ธ์ฝ๋ฉ ํ์ง ์๋๋ค.
๊ทธ๋ํ ์ธ์ฝ๋ฉ
adjacency matrix๋ก ํํํ๋ ๋ฐฉ๋ฒ์ด ์๋ค.
๊ฐ ๋
ธ๋ 0,1,2,3 ์ด ๊ฐ์ ์ฃ์ง๋ฅผ ๋ฆฌ์คํธ์ ๋ด๊ณ ๊ทธ๊ฒ๋ค์ ๋ค์ ๋ฆฌ์คํธ์ ๋ด๋ ๋ฐฉ๋ฒ.
[0,0,1,1] 0->2,3
[1,0,0,0] 1->0
[1,0,0,1] 2->0,3
[0,1,0,0] 3->1
๊ทผ๋ฐ ์ด๊ฑธ ๋๊ฐ์ด ๋ค์๊ณผ ๊ฐ์ด ๋ด์์ ๋ ์๋ค.
adjacency list๋ก
[2,3]
[0]
[0,3]
[1]
๋์ ์ฐจ์ด๊ฐ ์ค์ํ ์ ์๋ค๊ณ ํ๋๋ฐ ๊ต์ฌ์์๋ ๋ค๋ฃจ์ง ์๊ฒ ๋ค๊ณ ํ๋ค.
๋ํ์ ์ผ๋ก ํด๋นํ๋ ์ฃ์ง๊ฐ ์๋์ง ์กฐํํ ๋์๋ ๋ฆฌ์คํธ ๋ฐฉ๋ฒ์ด ๋ถ๋ฆฌํ๋ค : ํ๋ ฌ์ ๊ฒฝ์ฐ matrix[i][j]๋ก ํ๋ฒ์ ์กฐํ ๊ฐ๋ฅ. 1 or 0. ๊ทธ๋ฌ๋ ๋ฆฌ์คํธ์์๋ i๊ฐ ๊ฐ์ง๊ณ ์๋ ์ฃ์ง๋ค์ ๋ค ์กฐํํด์ j๊ฐ ์๋์ง ํ์ธํด์ผํ๋ค.
Computational tasks
์ปดํจํ , ๊ทธ๋ฌ๋๊น computational process๋ ์ ๋ ฅ -> ์ถ๋ ฅ ์ ์คํํ๋ ์ผ์ข ์ ํจ์๋ค.
specification, implementation
(mathematical functions ๊ณผ algorithms/programs ๋ถ๋ฆฌ.)
์ปดํจํ
์ ๋ํด์ ์ ๊ธฐํ ๋ ํฌ๊ฒ ๋ ๊ฐ์ง์ ์ฃผ์ ๊ฐ ์๋ค.
- ์ ๋ ฅ -> ์ถ๋ ฅ์ ์ด๋ป๊ฒ ๋งตํํ ๊ฒ์ธ๊ฐ?
- ๊ทธ๋ฆฌ๊ณ ๊ทธ๊ฑธ ์ด๋ป๊ฒ ๊ตฌํํ ๊ฒ์ธ๊ฐ
๋ ๋ค "์ด๋ป๊ฒ"๋ผ๋ ์ง๋ฌธ์ ํ์ง๋ง ๋ถ๋ช ํ ๊ตฌ๋ถ๋๋ค.
๊ทธ๋์ mathematical function ๊ณผ algorithm์ ๋ถ๋ฆฌ๋ผ๊ณ ๋ ํ๋ค.
1๋ฒ์ ์ํ์ ์ธ ์๋ฏธ๋ก 'ํจ์'์ ๋ํด์ ๋ฌป๋ ์ง๋ฌธ์ด๋ค. ๊ทธ๋๊น ์ปดํจํฐ๊ฐ ์ด๋ค ์ํ ํจ์๋ฅผ ์คํํด์ผ ๋๋์ง๋ฅผ, ์ฆ computational task๋ฅผ ๋ฌป๋๊ฒ์ด๋ค.
(2,3) -> 6, (3,4) ->12, (4,5)->20 ์ ์
๋ ฅ๋ ๋ ์๋ฅผ ๊ณฑํ ๊ฒฐ๊ณผ์ ์์ฐ์์ ๋งตํํ๋ ํจ์๋ค.
๋๋ฌด ๊ฐ๋จํด์ ์ ์๋ณด์ด์ง๋ง ์ด์จ๋ 'm๊ณผn์ ๊ณฑํ๊ธฐ' ๊ฐ ์๋ฌด๊ณ ,
๊ทธ๊ฑธ ๊ตฌํํ๋ ๋ฐฉ๋ฒ์ m์ n๋ฒ ๋ํ๊ธฐ, ํน์ ๊ต์ฌ ์์์ ๋์๋ ๋ฐฉ๋ฒ(grade-school algorithm)์ด ์๋ค. (์ด๊ฒ ์ ๋ถ๋ ์๋. ๋ ์๋ค.)
def mult1(x,y):
res = 0
while y>0:
res += x
y -= 1
return res
def mult2(x,y):
a = str(x) # represent x as string in decimal notation
b = str(y) # represent y as string in decimal notation
res = 0
for i in range(len(a)):
for j in range(len(b)):
res += int(a[len(a)-i])*int(b[len(b)-
j])*(10**(i+j))โช
return res
print(mult1(12,7))
# 84
print(mult2(12,7))
# 84
3๊ณผ 4๋ฅผ ๊ฐ์ง๊ณ 12๋ฅผ ๋ง๋ค์ด๋ด๋ ค๋ฉด ์ด๋คwhat ํจ์๊ฐ ํ์ํ์ง ์์์ผํ๊ณ (specification)
๊ทธ๊ฑธ ์ด๋ป๊ฒhow ๊ตฌํํ๋์ง ์์์ผํ๋ค(implementation).